package login;

import io.ClientSession;
import io.OperationResponse;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;

public class LenovoLogin {
	static String dlServerUrl = "http://passport.lenovo.com/interserver/authen/1.2/getaccountid";
	static int dlApp_id = 1191;
	static String realm = "10000786.realm.lenovoidapps.com";

	public static void Lenovologin(OperationResponse operationRequest,
			ClientSession session) {
		try {
			if (operationRequest.Parameters.size() < 2) {
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, null, ReturnCode.No, "");
				session.SendOperationResponse(response);
			}
			String id = (String) operationRequest.Parameters
					.get(ParameterType.UserID);
			String sessionid = (String) operationRequest.Parameters
					.get(ParameterType.UserPwd);
			String deviceID = (String) operationRequest.Parameters
					.get(ParameterType.DeviceID);
			String isGetIP = (String) operationRequest.Parameters
					.get(ParameterType.IsGetIP);
			String PhoneType=(String) operationRequest.Parameters
					.get(ParameterType.PhoneType);
			if (id.equals("zealm") && sessionid.equals("")) {
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, AppConfig.dicAppInfo,
						ReturnCode.GetServer, "");
				session.SendOperationResponse(response);
				return;

			}
			String  dicLenovo = LoginLenovo(id);
			if (dicLenovo.equals("")) {
				return;
			}
			id = "lenovo" + dicLenovo;
			operationRequest.Parameters.put(ParameterType.UserID, id);
			HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
			myPrams.put(ParameterType.UserID, id);
			OperationResponse responseID = new OperationResponse(
					operationRequest.OperationCode, myPrams, ReturnCode.HasID,
					"");
			session.SendOperationResponse(responseID);
			Util.sdkReceived(operationRequest, session, id, deviceID, isGetIP,"lenovo",PhoneType,"");

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static String  LoginLenovo(String mid)
			throws Exception {
		if (mid.isEmpty()) {
			return null;
		}
		String UserName="";
		String body = "lpsust=" + mid + "=&" + "realm=" + realm;
		body = new String(body.getBytes("gbk"), "utf-8");
		String result = Util.sendGet(dlServerUrl, body);// httppost方式调用服务器接口
		result = new String(result.getBytes("gbk"), "utf-8");
		UserName=ParseLenovoData(result);
		if(!UserName.equals("")){
			return UserName;
		}
		return null;

	}

	public static String ParseLenovoData(String result)
			throws ParserConfigurationException, SAXException, IOException {
		InputStream in_withcode = new ByteArrayInputStream(
				result.getBytes("UTF-8"));

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(in_withcode);
		Element element = document.getDocumentElement();
		String name = "";

		NodeList bookNodes = element.getElementsByTagName("Username");
		name = bookNodes.item(0).getTextContent();
		if(!name.equals("")){
			return name;
		}
		return "";

	}

}
